<template>
  <div>
    <el-dialog :title="user.isAdd?'用户添加':'用户修改'" :visible.sync="user.isshow" @closed="close">
      <el-form :model="form">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option label="请选择" value="shanghai"></el-option>
            <!-- 这里的value是id  因为接收到的角色的list中只有id -->
            <el-option v-for="item in list" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="user.isshow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="user.isAdd">确 定</el-button>
        <el-button type="primary" @click="edit" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { reqrolelist, reqroleinfo } from "../../request/api";
import {
  requseradd,
  reqrolelist,
  requserinfo,
  requseredit,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  mounted() {
    // 获取角色管理中的所有的角色  这里的select要用
    reqrolelist().then((res) => {
      if (res.data.code === 200) {
        this.list = res.data.list;
      }
    });
  },
  computed: {
    ...mapGetters({
      info: "user",
    }),
  },
  methods: {
    ...mapActions({
      changeUser: "reqlogin",
    }),

    //   点击空白处判断是否清空
    close() {
      if (!this.user.isAdd) {
        this.form = {};
      }
    },

    //   修改数据
    edit() {
      requseredit(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          if (this.info.uid === this.form.uid) {
            this.changeUser({});
            this.$router.push("/login");
            return;
          }
          // 清空数据
          this.empty();
          //   刷新数据
          this.$emit("init");
          //   弹框消失
          this.cancle();
        }
      });
    },

    //   拿到一条数据
    getOne(uid) {
      requserinfo({
        uid: uid,
      }).then((res) => {
        if (res.data.code === 200) {
          //   console.log(id);
          console.log(res);
          this.form = res.data.list;
          // 后面修改一条数据的时候需要用到uid  在此处添加uid
          this.form.uid = uid;
          this.form.password = "";
        }
      });
    },

    add() {
      requseradd(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          //   console.log(this.form);
          // form消失
          this.cancle();
          // 清空form
          this.form = {};
          //   刷新列表
          this.$emit("init");
        }
      });
    },

    cancle() {
      this.user.isshow = false;
    },

    // 清空数据
    empty() {
      this.form = {};
    },
  },
  props: ["user"],
  data() {
    return {
      list: [],
      isshow: true,
      form: {
        roleid: "",
        username: "",
        password: "",
        status: "",
      },
    };
  },
};
</script>

<style>
</style>